using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using TienTeTuong;
using log4net;

namespace TienTeTuong
{
	/// <summary>
	/// Data access layer class for Receiveemail
	/// </summary>
	class ReceiveemailDAO 
	{
       public bool AddNewReceiveEmail(ReceiveemailDTO email)
       {
           MySqlConnection SqlConnection = null;
           MySqlCommand MysqlCmd = null;
          // MySqlDataReader reader = null;

           try
           {

               SqlConnection = new MySqlConnection(Global.ConnectionString);
               if (SqlConnection.State == ConnectionState.Closed)
                   SqlConnection.Open();
               MysqlCmd = new MySqlCommand { Connection = SqlConnection };

               string strSQL = @"INSERT INTO receiveemail VALUES ('" + email.Email + "',";
               strSQL += "" + email.Active + "";
               strSQL += ")";

               MysqlCmd.CommandText = strSQL;

               MysqlCmd.ExecuteNonQuery();

               //reader.Close();
               MysqlCmd.Dispose();
               SqlConnection.Close();
               SqlConnection.Dispose();
               return true;

           }
           catch (Exception ex)
           {
               //if (reader != null) reader.Close();
               if (MysqlCmd != null)
                   MysqlCmd.Dispose();
               if (SqlConnection != null)
               {
                   SqlConnection.Close();
                   SqlConnection.Dispose();
               }
               return false;
           }
       }
       public bool CheckEmailExist(string email)
       {
           MySqlConnection SqlConnection = null;
           MySqlCommand MysqlCmd = null;
           MySqlDataReader reader = null;
           int toTal = 0;
           try
           {

               SqlConnection = new MySqlConnection(Global.ConnectionString);
               if (SqlConnection.State == ConnectionState.Closed)
                   SqlConnection.Open();
               MysqlCmd = new MySqlCommand { Connection = SqlConnection };

               string strSQL = @"SELECT email FROM receiveemail WHERE email='" + email + "'";
                             
               MysqlCmd.CommandText = strSQL;

               reader = MysqlCmd.ExecuteReader();
             if(reader.HasRows)
             {
                 return true;
             }
               reader.Close();
               MysqlCmd.Dispose();
               SqlConnection.Close();
               SqlConnection.Dispose();
               // return lstCategory;
               return false;
           }
           catch (Exception)
           {
               if (reader != null)
                   MysqlCmd.Dispose();
               if (SqlConnection != null)
               {
                   SqlConnection.Close();
                   SqlConnection.Dispose();
               }
               return true;
           }
       }


	}
}
